package cn.com.aiouyi.controller.web;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import cn.com.aiouyi.common.ExceptionCode;
import cn.com.aiouyi.common.Message;
import cn.com.aiouyi.constant.Constant;
import cn.com.aiouyi.entity.Member;
import cn.com.aiouyi.exception.system.SystemException;
import cn.com.aiouyi.service.MemberService;
import cn.com.aiouyi.shiro.AuthenticationToken;
import cn.com.aiouyi.shiro.Principal;
import cn.com.aiouyi.shiro.ShiroUtils;
import cn.com.aiouyi.util.CommUtil;

/**
 * Controller - 登录
 * @author hanson
 *
 */
@Controller(value = "loginController")
@RequestMapping(value = "/login")
@Validated
public class LoginController extends BaseController{

	@Resource(name = "memberServiceImpl")
	private MemberService memberService;
	
	@GetMapping()
    public String index(){
		/*Member m = new Member();
		m.setUsername("admin");
		m.setPassword(DigestUtils.md5Hex("123456"));
		memberService.save(m);*/
        return "/web/login";
    }
	
    @PostMapping("/submit")
    @ResponseBody
    public Message submit(HttpServletRequest request,
    		@NotBlank(message = "用户名不能为空") String username, 
    		@NotBlank(message = "密码不能为空") String password,
    		String verifyCode){
    	
    	String codeTrue = (String)WebUtils.getSessionAttribute(request, Constant.VERIFY_CODE_SESSION_KEY);
		if(StringUtils.isNotBlank(verifyCode) && StringUtils.isNotBlank(codeTrue) && codeTrue.equals(verifyCode)){
			
		}else{
			return Message.error("验证码错误");
		}
    	
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new AuthenticationToken(username, DigestUtils.md5Hex(password), Principal.Type.admin,
        		null, null, true, CommUtil.getIpAddr(request));
        try {
        	subject.login(token);
        } catch (LockedAccountException e) {
        	return Message.error(Message.CODE_LOGIN_FAILURE, "common.login.accountLockCount");
        } catch (UnknownAccountException e) {
        	return Message.error(Message.CODE_LOGIN_FAILURE, "common.login.unknownAccount");
        } catch (DisabledAccountException e) {
        	return Message.error(Message.CODE_LOGIN_FAILURE, "common.login.disabledAccount");
        } catch (IncorrectCredentialsException e) {
        	return Message.error(Message.CODE_LOGIN_FAILURE, "common.login.incorrectCredentials");
        } catch (AuthenticationException e) {
        	return Message.error(Message.CODE_LOGIN_FAILURE, "common.login.authentication");
        } 
        
        Principal principal = ShiroUtils.getPrincipal();
        if(principal == null){
        	throw new SystemException(ExceptionCode.LoginPrincipalError);
        }
        
        return Message.success();
    }
    
    @PostMapping("/logout")
    @ResponseBody
    public Message logout() {
    	Subject subject = SecurityUtils.getSubject();
    	if(subject != null){
    		subject.logout();
    	}
    	return Message.success();
    }

}
